{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import operator\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEACAYAAABbMHZzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHQ1JREFUeJzt3X10VPW97/H3F0jsTNJg0CBBdEJ84BmRVvHYUoOCVXoR\nlqvHQtHlA4fWem519aqIsk5NcWkP6NFivbS4CNb6EK32ULD1Fgo4x9Wu04KgwEVAxCYgLmMqD14s\nCxC+94/ZSRNMgGRPMjPZn9das5z920/fjMxn9vz2nv0zd0dERKKlW6YLEBGRzqfwFxGJIIW/iEgE\nKfxFRCJI4S8iEkEKfxGRCDrp8DezKjOrM7MNTdrmmtlmM3vLzH5tZkVN5t1rZtuC+Vemu3AREWm/\nthz5PwV8/Zi25cAQdx8BbAPuBTCzwcB1wCDgamC+mVn4ckVEJB1OOvzd/Y/AnmPaVrj70WDyz0C/\n4Pk1wAvu/pm715D6YLg4fLkiIpIO6ezzvwV4NXh+JrCzybxdQZuIiGSBtIS/mc0CDrt7dTq2JyIi\nHatH2A2Y2U3AeODyJs27gLOaTPcL2lpaXzcXEhFpB3dv97nUth75W/BITZhdBdwNXOPuB5sstxSY\nbGb5ZtYfOBdY3dpG3T1nH/fff3/Ga1D9ma8jivXncu1dof6wTvrI38yeByqA08xsB3A/cB+QD/wh\nuJjnz+5+m7u/bWa/At4GDgO3eTqqFRGRtDjp8Hf3b7fQ/NRxlv8x8OP2FCUiIh1Lv/ANqaKiItMl\nhKL6MyuX68/l2iH36w/LMt0bY2bqERIRaSMzw0Oc8A19tY+IyPGUlZVRW1ub6TJyViKRoKamJu3b\n1ZG/iHSo4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9/M/hD0DzY8Ngb/ndBk\nGf1ArBOZWSHwMnBH8A3g2DP+WXkFgJl9A6gLvr0c7+tsVtZPqptkJPC/3X0k8CmpLohcef1PJXXU\nnAD6kvoGMJUcqf84cq1eID25GfZSz+Ny93HHmx/8QGw8cHmT5l3AWU2m+wVt2WYXcHaT6Wyts1Hw\ndf1l4Bl3XxI015nZGe5eZ2Z9gI8yV+FxfQW4xszGk+py+KKZPQN8mCP1vw/sdPc3gulfkwr/XHn9\nxwLvuftuADNbDFxK7tTfoLV6cyV30pabmbza5ypSX+GvafhlcGApMDm4kqA/cC6wOhM1nsAa4Fwz\nS5hZPjCZVO3ZbBHwtrvPa9K2FLgpeH4jsOTYlbKBu9/n7me7ezmp13qVu98AvEJu1F8H7DSz84Om\nK4BN5MjrT6q75xIz+0JwIvEKUifes71+o/k3xdbqzdbcaVZ/WnPT3TPyIHVCohZYFzzmN5l3L/Au\nqZOSV2aqxpP4G64CtgZ/y8xM13OCWr8CHAHeAt4MXvOrgF7AiuDvWA6cmulaT+JvuQxYGjzPmfpJ\nXeGzJvh/8J9Azxyr//7gPbmB1MnSvGyuH3ge+AA4SOrD62aguLV6sy13Wqk/bbmpH3mJiERQtlzt\nIyIinUjhLyISQQp/EZEIUviLiESQwl9EJIIU/iIiEaTwFxGJIIW/iEgE/X/gJBwvqhL3MwAAAABJ\nRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1d716e2c8d0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#已知数据\n",
    "x1 = np.array([3,2,1])\n",
    "y1 = np.array([104,100,81])\n",
    "x2 = np.array([101,99,98])\n",
    "y2 = np.array([10,5,2])\n",
    "\n",
    "scatter0 = plt.scatter(x1,y1,c='r')\n",
    "scatter1 = plt.scatter(x2,y2,c='b')\n",
    "\n",
    "#未知数据\n",
    "x = np.array([109])\n",
    "y = np.array([9])\n",
    "scatter2 = plt.scatter(x,y,c='k')\n",
    "\n",
    "plt.legend(handles = [scatter0,scatter1,scatter2],labels=['label0','label1','label2'],loc ='best')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x_data = np.array([[3,104],\n",
    "                  [2,100],\n",
    "                  [1,81],\n",
    "                  [101,10],\n",
    "                  [99,5],\n",
    "                  [98,2]])\n",
    "y_data = np.array(['A','A','A','B','B','B'])\n",
    "x_test = np.array([[109,9]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data_size = x_data.shape[0]\n",
    "x_data_size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[109,   9],\n",
       "       [109,   9],\n",
       "       [109,   9],\n",
       "       [109,   9],\n",
       "       [109,   9],\n",
       "       [109,   9]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#复制x_test\n",
    "np.tile(x_test,(x_data_size,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[106, -95],\n",
       "       [107, -91],\n",
       "       [108, -72],\n",
       "       [  8,  -1],\n",
       "       [ 10,   4],\n",
       "       [ 11,   7]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求差值\n",
    "diffMat = np.tile(x_test,(x_data_size,1))-x_data\n",
    "diffMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11236,  9025],\n",
       "       [11449,  8281],\n",
       "       [11664,  5184],\n",
       "       [   64,     1],\n",
       "       [  100,    16],\n",
       "       [  121,    49]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求平方\n",
    "sqdiffMat = diffMat**2\n",
    "sqdiffMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20261, 19730, 16848,    65,   116,   170])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求和\n",
    "sqDistances = sqdiffMat.sum(axis=1)\n",
    "sqDistances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 142.34113952,  140.4635184 ,  129.79984592,    8.06225775,\n",
       "         10.77032961,   13.03840481])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#开方\n",
    "distances = sqDistances**0.5\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 2, 1, 0], dtype=int64)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#排序\n",
    "sortedDistances = distances.argsort()\n",
    "sortedDistances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A': 2, 'B': 3}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classCount = {}\n",
    "k = 5\n",
    "for i in range(k):\n",
    "    #获取标签\n",
    "    votelabel = y_data[sortedDistances[i]]\n",
    "    #统计每个标签的个数\n",
    "    classCount[votelabel] = classCount.get(votelabel,0)+1\n",
    "classCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', 3), ('A', 2)]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#根据operator.itemgetter(1)  -> 第一个值对classCount排序，然后取倒序\n",
    "sortedclassCount = sorted(classCount.items(),key = operator.itemgetter(1),reverse = True)\n",
    "sortedclassCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'B'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knnclass = sortedclassCount[0][0]\n",
    "knnclass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
